//
//  YWDailyReportTVC.m
//  maintain
//
//  Created by 烟雨云渊 on 2017/6/6.
//  Copyright © 2017年 ewin. All rights reserved.
//

#import "YWDailyReportTVC.h"
#import "MJRefresh.h"
#import "YWCommonTool.h"
#import "NSDate+YYAdd.h"
#import "YWMacro.h"
#import "UIColor+CustomColors.h"

#import "YWDailyReport.h"
#import "YWDailyReportCountCell.h"
#import "YWWorkReminderManager.h"
#import "YWDailyReportDetailTVC.h"

@interface YWDailyReportTVC ()
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) NSMutableArray *dataSource;
@end

@implementation YWDailyReportTVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"工作日报";
    [self setupTableView];
}

- (void)setupTableView{
    self.view.backgroundColor = [UIColor customBackgroudColor];
    [self.tableView registerClass:[YWDailyReportCountCell class] forCellReuseIdentifier:YWDailyReportCountCellIdentifier];
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.sectionFooterHeight = 0.01;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.estimatedRowHeight = 44.0f;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshFootor)];
    self.tableView.mj_footer.automaticallyHidden = YES;
}

- (void)refreshFootor
{
    [self requestData:YWTableViewFreshTypeUp];
}

- (void)requestData:(YWTableViewFreshType)refreshType
{
    if (refreshType == YWTableViewFreshTypeUp) {
       self.page++;
    }
    YWWeakSelf(weakSelf);
    dispatch_async(dispatch_get_main_queue(), ^{
        [weakSelf.tableView.mj_footer endRefreshing];
        weakSelf.dataSource = nil;
        [weakSelf.tableView reloadData];
    });
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.dataSource count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSMutableArray *buildings = [NSMutableArray array];
    NSMutableDictionary * dic = self.dataSource[section];
    buildings = [dic objectForKey:@"buildingDailyArray"];
    return buildings.count + 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSMutableDictionary * dailyDic = self.dataSource[indexPath.section];
    NSMutableArray * buildingDailyArray = [NSMutableArray array];
    buildingDailyArray = [dailyDic objectForKey:@"buildingDailyArray"];
    static NSString *identifier = @"YWDailyReportTVCCellIdentifier";
    if (indexPath.row ==0 || indexPath.row == 1) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        if (indexPath.row == 0) {
            YWDailyReport * daily = buildingDailyArray[0][0];
            cell.textLabel.text = [NSString stringWithFormat:@"%@工作日报",daily.analysisDate];
            return cell;
        }else{
            cell.textLabel.text = @"任务完成率统计";
            return cell;
        }
    }else{
        YWDailyReportCountCell * reportCell = [tableView dequeueReusableCellWithIdentifier:YWDailyReportCountCellIdentifier];
        reportCell.dailyReportArray = buildingDailyArray[indexPath.row-2];
        return reportCell;
    }
}

- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section{
    return 35;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    static NSString* HeaderIdentifier = @"YWDailyReportTVCForHeaderInSection";
    UITableViewHeaderFooterView* headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HeaderIdentifier];
    if (headerView == nil) {
        headerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:HeaderIdentifier];
        headerView.contentView.backgroundColor = [UIColor customBackgroudColor];
        UILabel* label = [[UILabel alloc] init];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor blackColor];
        label.tag = 2016;
        label.font = KTitleFont;
        [headerView addSubview:label];
    }
    
    NSString *headText = [self titleForHeaderInSection:section];
    UILabel* label = [headerView viewWithTag:2016];
    label.text = headText;
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor customTextGrayBlackColor];
    label.frame = CGRectMake(15, 0, SCREEN_WIDTH - 2 * 15, 35);
    return headerView;
}

- (NSString *)titleForHeaderInSection:(NSInteger)section{
    NSMutableDictionary * dic = self.dataSource[section];
    NSMutableArray * buildingDailyArray = [NSMutableArray array];
    buildingDailyArray = [dic objectForKey:@"buildingDailyArray"];
    YWDailyReport * daily = buildingDailyArray[0][0];
    NSString * timeString = daily.analysisDate;
    return timeString;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row>1) {
        NSMutableDictionary * dailyDic = self.dataSource[indexPath.section];
        NSMutableArray * buildingDailyArray = [NSMutableArray array];
        buildingDailyArray = [dailyDic objectForKey:@"buildingDailyArray"];
        YWDailyReportDetailTVC * dailyDetailVC = [[YWDailyReportDetailTVC alloc] init];
        dailyDetailVC.dailyReportArray = buildingDailyArray[indexPath.row-2];
        [self.navigationController pushViewController:dailyDetailVC animated:YES];
    }
}

#pragma mark - 懒加载

- (NSMutableArray*)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];//将数据库取出来的dictionary加上
        NSMutableDictionary *dic = [[YWWorkReminderManager shareManager] dailyReportArrayWithDailyDate:@"2017-06-05"];
        [_dataSource addObject:dic];
    }
    return _dataSource;
}


@end
